package com.smartleanx.module.hrm.app.risk;

import com.smartleanx.framework.datapermission.core.annotation.DataPermission;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 风险点检出执行器
 *
 * @author tengcj
 * @since 2024/6/6
 */
@Slf4j
@Component
public class RiskHandleService implements ApplicationContextAware {

    Map<Long, RiskHandle> handleMap = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, RiskHandle> serviceMap = applicationContext.getBeansOfType(RiskHandle.class);
        handleMap = serviceMap.values().stream().collect(Collectors.toMap(RiskHandle::type, riskHandle -> riskHandle));
    }

    @DataPermission(enable = false)
    @Transactional(rollbackFor = Exception.class)
    public void execute(Set<Long> recordIdList, Long riskType) {
        RiskHandle riskHandle = handleMap.get(riskType);
        if (riskHandle != null) {
            riskHandle.handleRisk(recordIdList);
        }
    }

}
